
<template>
  <el-dialog v-model="ifShow" :title="modelTitle" width="1000" :before-close="handleCancel">
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="auto" class="demo-ruleForm" status-icon>
      <el-form-item label="标题" prop="title">
        <el-input placeholder="请输入" v-model="formData.title"></el-input>
      </el-form-item>
      <el-form-item label="答案" prop="result">
        <el-input placeholder="请输入" v-model="formData.result"></el-input>
      </el-form-item>
      <el-form-item label="选项">
        <div class="flex_box">
          <div class="item" v-for="(item, i) in configArr" :key="item.id">
            <el-input style="width: 280px" placeholder="请输入" v-model="item.text"></el-input>
            <el-button style="margin-left: 20px" type="danger" @click="handleRemove(i)">删除</el-button>
            <el-button style="position: absolute; right: -80px" v-if="i == configArr.length - 1" type="primary" @click="handleAdd">新增</el-button>
          </div>
        </div>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" @click="handleOk">确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from "vue";
import ActivityApi from "@/api/activity";
import CommonApi from "@/api/common";
import { channelArr, bizTypeArr, orderTypeArr, platFormArr, carTypeArr } from "@/enums/common";
const carOptions = ref();
const props = defineProps<{}>();
const emit = defineEmits(["success"]);
const formRef: any = ref(null);
let formData: any = ref({
  id: null,
  title: null,
  result: null,
  option: null,
});
const rules: any = reactive({
  title: [{ required: true, message: "请输入!" }],
  result: [{ required: true, message: "请输入!" }],
});
const configArr: any = ref([{ text: "", id: Date.now() + Math.random().toString(16).slice(2) }]);
const ifShow = ref(false);
const modelTitle = ref();
const cityOptions = ref();
function edit(title: any, row: any) {
  modelTitle.value = title;
  ifShow.value = true;
  if (title == "编辑") {
    configArr.value = row.option.split(",").map((text) => ({
      text: text,
      id: Date.now() + Math.random().toString(16).slice(2),
    }));
  }
  if (title == "新增") {
    formData.value = { id: "", title: "", result: "", option: null };
  }
  //赋值
  Object.assign(formData.value, row);
}

function handleAdd(code: any) {
  configArr.value.push({
    text: "",
    id: Date.now() + Math.random().toString(16).slice(2),
  });
}
function handleRemove(index: any) {
  if (configArr.value.length !== 1) {
    configArr.value.splice(index, 1);
  }
}

/**
 * 确定按钮点击事件
 */
async function handleOk() {
  await formRef.value.validate((valid: any, fields: any) => {
    if (valid) {
      let model: any = formData.value;
      model.option = configArr.value.map((item) => item.text).join(",");

      ActivityApi.QuestionEdit(model).then((res: any) => {
        if (res.code == 0) {
          ElMessage.success(res.message);
          formRef.value.resetFields();
          emit("success");
          handleCancel();
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
}

/**
 * 取消按钮回调事件
 */
function handleCancel() {
  formRef.value.resetFields();
  configArr.value = [
    {
      text: "",
      id: Date.now() + Math.random().toString(16).slice(2),
    },
  ];
  ifShow.value = false;
}

onMounted(() => {
  CommonApi.getCity({ pageNo: 1, pageSize: 9999 }).then((data) => {
    if (data.code == 0) {
      cityOptions.value = data.result;
    }
  });
});

defineExpose({
  edit,
});
</script>
<style lang="scss" scoped>
.flex_box {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  margin-bottom: 14px;
  position: relative;
  .item {
    display: flex;
    margin-top: 16px;
  }
}
</style>
